package com.jin.api.v1;

import com.jin.Exception.http.NotFoundException;
import com.jin.dto.TokenDTO;
import com.jin.dto.TokenGetDTO;
import com.jin.service.WxAuthenticationService;
import com.jin.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

//让用户获取自己的token
@RestController
@RequestMapping("/token")
public class TokenController {
    @Autowired
    private WxAuthenticationService wxAuthenticationService;

    @PostMapping("")
    public Map<String,String> getToken(@RequestBody @Validated TokenGetDTO userData){
        Map<String,String> map = new HashMap<>();
        //token为令牌
        String token = null;
        //根据登录的类型来进行分别的处理
        switch (userData.getLogintype()){
            case USER_WX:
                token = wxAuthenticationService.code2Session(userData.getAccount());
                break;
            case USER_EMAIL:
                break;
            default:
                throw new NotFoundException(10003);
        }
        map.put("token",token);
        return map;
    }

    //查看token是否过期的接口
    @PostMapping("/verify")
    public Map<String,Boolean> verify(@RequestBody TokenDTO tokenDTO){
        Map<String,Boolean> map = new HashMap<>();
        Boolean valid = JwtToken.verifyToken(tokenDTO.getToken());
        map.put("is_valid",valid);
        return map;
    }
}
